package copyengine.ui
{
    import copyengine.infoTree.meta.CESDisplayObjectMeta;
    import copyengine.infoTree.meta.CESFileMeta;
    import copyengine.log.CELog;
    import copyengine.resource.CEResUtils;
    import copyengine.resource.resProxy.CEResProxy;
    import copyengine.ui.component.button.CEButton;
    import copyengine.ui.component.button.CESelectableButton;
    import copyengine.ui.component.button.animation.CEColorFilterButtonAnimation;
    import copyengine.ui.component.button.animation.CEColorFilterSelectableButtonAnimation;
    import copyengine.ui.component.button.animation.CEFrameSelectableButtonAnimation;
    import copyengine.ui.component.progress.CEProgressBar;
    import copyengine.ui.component.progress.animation.CEChangeSizeProgressBarAnimation;
    import copyengine.ui.component.progress.animation.CEMaskProgressBarAnimation;
    import copyengine.ui.component.scrollablePanel.common.layout.CEScrollableLayoutStandard;
    import copyengine.ui.component.scrollablePanel.element.list.displayContainerSharePool.CEListElementSharePool;
    import copyengine.ui.component.scrollablePanel.list.CEList;
    import copyengine.ui.component.scrollablePanel.scrollPanel.CEScrollPanel;
    import copyengine.ui.component.tabbar.CETabbar;
    import copyengine.ui.display.movieClip.CEMovieClip;
    import copyengine.ui.display.ph.CEPlaceHolder;

    import starling.display.DisplayObject;
    import starling.display.DisplayObjectContainer;

    public class CEGuiUtils
    {
        public function CEGuiUtils() {
        }


        //=====================
        //== Button
        //=====================

        public static function getFrameSelectableButton(_mc:DisplayObject, _onClickCallback:Function = null, _isClickToSelected:Boolean = true):CESelectableButton {
            var button:CESelectableButton = new CESelectableButton(_mc);
            button.onClickCallBack = _onClickCallback;

            var animation:CEFrameSelectableButtonAnimation = new CEFrameSelectableButtonAnimation();
            animation.initialize(_mc);
            button.animation = animation;
            button.isClickToSelect = _isClickToSelected;

            return button;
        }


        public static function getColorFilterButton(_mc:DisplayObject, _onClickCallback:Function = null):CEButton {
            var button:CEButton = new CEButton(_mc);
            button.onClickCallBack = _onClickCallback;

            var animation:CEColorFilterButtonAnimation = new CEColorFilterButtonAnimation();
            animation.initialize(_mc);
            button.animation = animation;

            return button;
        }

        public static function getColorFilterSelectableButton(_mc:DisplayObject, _onClickCallback:Function = null, _isClickToSelected:Boolean = true):CESelectableButton {
            var button:CESelectableButton = new CESelectableButton(_mc);
            button.onClickCallBack = _onClickCallback;

            var animation:CEColorFilterSelectableButtonAnimation = new CEColorFilterSelectableButtonAnimation();
            animation.initialize(_mc);
            button.animation = animation;
            button.isClickToSelect = _isClickToSelected;

            return button;
        }

        //=====================
        //== Tabbar
        //=====================

        public static function getColorFilterTabbar(_mc:DisplayObjectContainer, _defaultTab:String, _onTabChangeCallback:Function = null):CETabbar {
            var subButtonsVector:Vector.<CESelectableButton> = new Vector.<CESelectableButton>();
            var totalChildNum:int = _mc.numChildren;
            for (var childIndex:int = 0; childIndex < totalChildNum; childIndex++) {
                var subChild:DisplayObject = _mc.getChildAt(childIndex);
                if (subChild is CEMovieClip) {
                    var subBtn:CESelectableButton = getColorFilterSelectableButton(subChild);
                    subButtonsVector.push(subBtn);
                }
            }
            var tabbar:CETabbar = new CETabbar(_mc, subButtonsVector, _defaultTab);
            tabbar.onTabChangeCallback = _onTabChangeCallback;
            return tabbar;
        }

        public static function getFrameTabbar(_mc:DisplayObjectContainer, _defaultTab:String, _onTabChangeCallback:Function = null):CETabbar {
            var subButtonsVector:Vector.<CESelectableButton> = new Vector.<CESelectableButton>();
            var totalChildNum:int = _mc.numChildren;
            for (var childIndex:int = 0; childIndex < totalChildNum; childIndex++) {
                //Frame的按钮里面不是CEMovieClip
                var subChild:DisplayObject = _mc.getChildAt(childIndex);
                var subBtn:CESelectableButton = getFrameSelectableButton(subChild);
                subButtonsVector.push(subBtn);
            }
            var tabbar:CETabbar = new CETabbar(_mc, subButtonsVector, _defaultTab);
            tabbar.onTabChangeCallback = _onTabChangeCallback;
            return tabbar;
        }

        //=====================
        //== Progress
        //=====================

        public static function getMaskProgressBar(_mc:DisplayObject, _dir:int, _defaultPercent:Number = 100):CEProgressBar {
            var progressBar:CEProgressBar = new CEProgressBar(_mc);
            var animation:CEMaskProgressBarAnimation = new CEMaskProgressBarAnimation();
            animation.initialize(_mc, _dir);
            progressBar.animation = animation;
            progressBar.percent = _defaultPercent;
            return progressBar;
        }

        public static function getChangeSizeProgressBar(_mc:DisplayObject, _dir:int, _defaultPercent:Number = 100):CEProgressBar {
            var progressBar:CEProgressBar = new CEProgressBar(_mc);
            var animation:CEChangeSizeProgressBarAnimation = new CEChangeSizeProgressBarAnimation();
            animation.initialize(_mc, _dir);
            progressBar.animation = animation;
            progressBar.percent = _defaultPercent;
            return progressBar;
        }

        //=====================
        //== ScrollPanel
        //=====================

        public static function getScrollPanel(_mc:DisplayObjectContainer, _dir:int, _isLockInView:Boolean):CEScrollPanel {
            var mask:CEPlaceHolder = _mc.getChildByName("mask") as CEPlaceHolder;
            if (mask == null) {
                CELog.err("CEGuiUtils->getScrollPanel target not contain any child named `mask`  ");
                return null;
            }
            var scrollPanel:CEScrollPanel = new CEScrollPanel(_mc, new CEScrollableLayoutStandard(), _dir, _isLockInView, mask.width, mask.height);
            mask.parent.removeChild(mask, true);
            return scrollPanel;
        }

        //=====================
        //== List
        //=====================

        /**
         *
         * @param _listMc
         * @param _cellRenderExportName               对应CellRender的导出类名字
         * @param _flaName                                      对应CellRender所在FLA文件的名字
         * @param _resProxy
         * @param _dir                                               CEConst.DIR_HORIZONTAL || CEConst.DIR_VERTICAL.
         * @param _isLockInView                               是否锁定在有回弹特效
         * @return
         */
        public static function getList(_listMc:DisplayObjectContainer, _cellRenderExportName:String, _flaName:String,
                                       _resProxy:CEResProxy, _dir:int, _isLockInView:Boolean):CEList {

            //在CEInfoTreeListElementDisplayContainerSharePool里面需要使用CESDisplayObjectMeta去构建CellRender(在CellRender不够的情况下)
            //但是为了不再外面直接操作CEResProxy getDecompressedObj 就让外面传入 导出类的名字以及FLA的名字
            //这里有个约定
            //1` 传入的ResProxy 需要已经加载了 _flaName+".ce"文件
            var fileMeta:CESFileMeta = CEResUtils.getDecompressedObj(_flaName + ".ce", _resProxy);
            var mcMeta:CESDisplayObjectMeta = fileMeta.getSymbolByName(_cellRenderExportName);

            var sharePool:CEListElementSharePool = new CEListElementSharePool(mcMeta, _resProxy);
            var list:CEList = new CEList(_listMc, new CEScrollableLayoutStandard(),
                    _dir, _isLockInView,
                    _listMc.width, _listMc.height, sharePool);

            return list;
        }

    }
}
